home *** CD-ROM | disk | FTP | other *** search
/ Sound Fx / Sound Fx.iso / Software / UNZIPED / DWSTK / PLAYDSP.C < prev    next >
C/C++ Source or Header  |  1996-10-10  |  7KB  |  343 lines

  1. /******************************************************************************
  2. File:          playdsp.c
  3. Version:     2.22
  4. Tab stops: every 2 columns
  5. Project:     DWD Player
  6. Copyright: 1994-1995 DiamondWare, Ltd.    All rights reserved.
  7. Written:     Erik Lorenzen
  8. Purpose:     Contains simple example code to show how to load/play a .DWD file
  9. History:     95/09/16 EL Converted to playdsp.c from playdwd.c
  10.                      95/10/16 EL Finalized for 2.10
  11.                      95/10/18 EL Finalized for 2.20, changed volume to 95%
  12.                      95/12/07 EL Finalized for 2.21, no changes
  13.                      96/10/10 EL Finalized for 2.22, no changes
  14.  
  15. Notes
  16. -----
  17. This code isn't really robust when it comes to standard error checking
  18. and particularly recovery, software engineering technique, etc.  The STK will
  19. handle songs larger than 64K (but not digitized sounds).    Obviously, you'd
  20. need to fread() such a file in chunks, or write some sort of hfread() (huge
  21. fread).  Also, exitting and cleanup is not handled robustly in this code.
  22. The code below can only be validated by extremely careful scrutiny to make
  23. sure each case is handled properly.  A better method would the use of C's
  24. atexit function.
  25.  
  26. But all such code would make this example file less clear; its purpose was
  27. to illustrate how to call the STK, not how to write QA-proof software.
  28. ******************************************************************************/
  29.  
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <conio.h>
  35. #include <sys\types.h>
  36. #include <sys\stat.h>
  37. #include <ctype.h>
  38.  
  39. #include "dws.h"
  40. #include "err.h"
  41. #include "dwdsp.h"
  42.  
  43.  
  44.  
  45. static word LoadFile(char *fname, byte **ptr, dword *len)
  46. {
  47.     FILE *fp=NULL;
  48.     struct stat st;
  49.  
  50.     if (!stat(fname, &st))                                         //get info on the file
  51.     {
  52.         *ptr = (byte *)malloc((size_t)st.st_size);
  53.  
  54.         if (*ptr == NULL)
  55.         {
  56.             printf("\nOutta memory pal!\n");
  57.             goto RETFAIL;
  58.         }
  59.  
  60.         fp = fopen(fname, "rb");
  61.  
  62.         if (fp == NULL)
  63.         {
  64.             printf("\nUnable to open %s\n",fname);
  65.             goto RETFAIL;
  66.         }
  67.  
  68.         if (fread(*ptr, (word)st.st_size, 1, fp) != 1)
  69.         {
  70.             printf("\nError Reading File!!! \n");
  71.             goto RETFAIL;
  72.         }
  73.  
  74.         if (fclose(fp))
  75.         {
  76.             printf("\nError Closeing File!!! \n");
  77.             //don't goto RETFAIL
  78.             return(0);
  79.         }
  80.  
  81.         *len = (dword)st.st_size;
  82.  
  83.         return(1);
  84.     }
  85.     else
  86.     {
  87.         printf("\nBad filename.\n");
  88.         return(0);
  89.     }
  90.  
  91.     RETFAIL:
  92.  
  93.     if (fp != NULL)
  94.     {
  95.         if (fclose(fp))
  96.         {
  97.             printf("\nError Closeing File!!! \n");
  98.         }
  99.     }
  100.  
  101.     if (*ptr != NULL)
  102.     {
  103.         free(*ptr);
  104.     }
  105.  
  106.     return(0);
  107. }
  108.  
  109.  
  110. static void InitSTK(byte *sound)
  111. {
  112.     dws_DETECTOVERRIDES dov;
  113.     dws_DETECTRESULTS     dres;
  114.     dws_IDEAL                     ideal;
  115.  
  116.     /*
  117.      . We need to set every field to -1 in dws_DETECTOVERRIDES struct;
  118.      . this tells the STK to autodetect everything.  Any other value
  119.      . overrides the autodetect routine, and will be accepted on
  120.      . faith, though the STK will verify it if possible.
  121.     */
  122.     dov.baseport = (word)-1;
  123.     dov.digdma     = (word)-1;
  124.     dov.digirq     = (word)-1;
  125.  
  126.     if (!dws_DetectHardWare(&dov, &dres))
  127.     {
  128.         err_Display(dws_ErrNo(), err_DWS);
  129.         exit(-1);
  130.     }
  131.  
  132.     if (!(dres.capability & dws_capability_DIG))
  133.     {
  134.         if ((dres.baseport != 0x388) && (dres.baseport != (word)-1))
  135.         {
  136.             printf("The sound hardware supports digitized sound playback.\n");
  137.             printf("We couldn't find the DMA channel and/or IRQ level.\n");
  138.         }
  139.         else
  140.         {
  141.             printf("DIG support not found\n");
  142.         }
  143.  
  144.         exit(-1);
  145.     }
  146.  
  147.     /*
  148.      . The "ideal" struct tells the STK how you'd like it to initialize the
  149.      . sound hardware.    In all cases, if the hardware won't support your
  150.      . request, the STK will go as close as possible.  For example, not all
  151.      . sound boards will support all sampling rates (some only support five or
  152.      . six discrete rates).
  153.     */
  154.     ideal.musictyp     = 0;      /* 0=No music, 1=OPL2 */
  155.     ideal.digtyp         = 8;      /* 0=No Dig, 8=8bit */
  156.     ideal.dignvoices = 16;     /* number of voices (up to 16) */
  157.     ideal.dignchan     = 1;      /* 1=mono */
  158.  
  159.     if (!dws_DGetRateFromDWD(sound, &ideal.digrate)) //set ideal.digrate
  160.     {
  161.         err_Display(dws_ErrNo(), err_DWS);
  162.         exit(-1);
  163.     }
  164.  
  165.     if (!dws_Init(&dres, &ideal))
  166.     {
  167.         err_Display(dws_ErrNo(), err_DWS);
  168.         exit(-1);
  169.     }
  170.  
  171.     /* Set Master Volume to about 95% max */
  172.     if (!dws_XMaster(242))
  173.     {
  174.         err_Display(dws_ErrNo(), err_DWS);
  175.     }
  176. }
  177.  
  178.  
  179. static void KillSTK(void)
  180. {
  181.     if (!dws_Kill())
  182.     {
  183.         /*
  184.          . If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  185.          . or dws_NOTINITTED.
  186.         */
  187.         err_Display(dws_ErrNo(), err_DWS);
  188.  
  189.         if (dws_ErrNo() == dws_Kill_CANTUNHOOKISR)
  190.         {
  191.             /*
  192.              . Unlikely but if it's dws_Kill_CANTUNHOOKISR you might want to
  193.              . consider a way of allowing the user to handel the tsr and then
  194.              . try dws_kill again.
  195.             */
  196.         }
  197.     }
  198. }
  199.  
  200.  
  201. static word GetInput(word *volume, dword *length)
  202. {
  203.     word quit=0;
  204.     word needkey;
  205.     int key;
  206.  
  207.     do
  208.     {
  209.         needkey = 0;
  210.  
  211.         key = getch();
  212.  
  213.         switch (tolower(key))
  214.         {
  215.             case 'q':
  216.             {
  217.                 quit = 1;
  218.                 break;
  219.             }
  220.             case 'f':
  221.             {
  222.                 (*length)--;
  223.                 printf("Percent of original sound length %u\n", *length);
  224.                 break;
  225.             }
  226.             case 's':
  227.             {
  228.                 (*length)++;
  229.                 printf("Percent of original sound length %u\n", *length);
  230.                 break;
  231.             }
  232.             case 'u':
  233.             {
  234.                 (*volume) += 0x10;
  235.                 printf("Percent of original sound volume 0x%x\n", *volume);
  236.                 break;
  237.             }
  238.             case 'd':
  239.             {
  240.                 (*volume) -= 0x10;
  241.                 printf("Percent of original sound volume 0x%x\n", *volume);
  242.                 break;
  243.             }
  244.             default:
  245.             {
  246.                 needkey = 1;
  247.             }
  248.         }
  249.  
  250.     } while (needkey);
  251.  
  252.     return (quit);
  253. }
  254.  
  255.  
  256. static void DoDSPExample(byte *sound, dword srclen)
  257. {
  258.     dws_DPLAY dplay;
  259.     byte *soundcopy;
  260.     dword length=100;                                                 //full length
  261.     word    volume=dwdsp_IDENTITY;                            //full volume
  262.     dword newlen;
  263.     word quit;
  264.  
  265.     printf("q - quit\n");
  266.     printf("f - faster\n");
  267.     printf("s - slower\n");
  268.     printf("u - vol up\n");
  269.     printf("d - vol down\n");
  270.  
  271.     do
  272.     {
  273.         newlen = (srclen * length) / 100;
  274.  
  275.         soundcopy = (byte *)malloc((size_t)newlen);
  276.  
  277.         if (soundcopy == NULL)
  278.         {
  279.             printf("\n\nOutta memory pal!\n\n");
  280.             goto KillIt;
  281.         }
  282.  
  283.         if (!dwdsp_ChngLen(soundcopy, sound, newlen))
  284.         {
  285.             err_Display(dwdsp_ErrNo(), err_DWDSP);
  286.             goto KillIt;
  287.         }
  288.  
  289.         if (!dwdsp_ChngVol(soundcopy, soundcopy, volume))
  290.         {
  291.             err_Display(dwdsp_ErrNo(), err_DWDSP);
  292.             goto KillIt;
  293.         }
  294.  
  295.         dplay.snd          = soundcopy;
  296.         dplay.count      = 0;
  297.         dplay.priority = 1000;
  298.         dplay.presnd     = 0;
  299.  
  300.         if (!dws_DPlay(&dplay))
  301.         {
  302.             err_Display(dws_ErrNo(), err_DWS);
  303.             goto KillIt;
  304.         }
  305.  
  306.         quit = GetInput(&volume, &length);
  307.  
  308.         if (!dws_DDiscardAO(soundcopy))
  309.         {
  310.             err_Display(dws_ErrNo(), err_DWS);
  311.             goto KillIt;
  312.         }
  313.  
  314.         free (soundcopy);
  315.  
  316.     } while (!quit);
  317.  
  318.     KillIt:;
  319. }
  320.  
  321.  
  322. void main(int argc, char **argv)
  323. {
  324.     dword srclen;
  325.     byte *sound;
  326.  
  327.     printf("\nPLAYDSP 2.22 is Copyright 1994-95 DiamondWare, Ltd.\n");
  328.     printf("All rights reserved.\n\n\n");
  329.  
  330.     if (argc < 2)
  331.     {
  332.         printf("Usage playdsp <dwd-file>\n");
  333.         exit(-1);
  334.     }
  335.  
  336.     if (LoadFile(argv[1], &sound, &srclen))
  337.     {
  338.         InitSTK(sound);
  339.         DoDSPExample(sound, srclen);
  340.         KillSTK();
  341.     }
  342. }
  343.